﻿using Sustainalytics.ClientDW.Entities;
using Sustainalytics.Entities;
using System;
using System.Collections.Generic;
using Sustainalytics.Claims;
using Sustainalytics.ScreeningTool.Service.Utility;
using System.Web.Http;
using Sustainalytics.ESG.Entities;
using Sustainalytics.ScreeningTool.BL;

namespace Sustainalytics.ScreeningTool.Service
{
    public class TemplateCriteriaDto
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public CriteriaTypeEnum CriteriaType { get; set; }
        public bool IsScreenable { get; set; }

        public string CriteriaEntityField { get; set; }

        public Guid CriteriaLevel2 { get; set; }
        public List<AnswerCategoryDto> AnswerCategories { get; set; }

        public static TemplateCriteriaDto CreateLessTemplateCriteria(TemplateCriteria templateCriteria, object indicatorTemplate)
        {
            TemplateCriteriaDto templateCriteriaDto = new TemplateCriteriaDto
            {
                Id = templateCriteria.Id,
                Name = templateCriteria.Name,
                CriteriaLevel2 = templateCriteria.CriteriaLevel2Id,
                CriteriaEntityField = templateCriteria.CriteriaEntityField,
                CriteriaType = templateCriteria.CriteriaType,
                IsScreenable = templateCriteria.IsScreenable,
            };

            if (indicatorTemplate != null)
            {
                if (indicatorTemplate.GetType() == typeof(IndicatorTemplate))
                {
                    templateCriteriaDto.AnswerCategories = ((IndicatorTemplate)indicatorTemplate).AnswerCategories.ConvertAll(x => AnswerCategoryDto.ConvertAnswerCategoriesToDto(x));
                }
                if (indicatorTemplate.GetType() == typeof(GovernanceIndicatorTemplate))
                {
                    templateCriteriaDto.AnswerCategories = ((GovernanceIndicatorTemplate)indicatorTemplate).AnswerCategories.ConvertAll(x => AnswerCategoryDto.ConvertAnswerCategoriesToDto(x));
                    templateCriteriaDto.AnswerCategories.Add(new AnswerCategoryDto() { Text = "Off", Score = -1, Code = "Z" });
                }
                if(indicatorTemplate.GetType() == typeof(CriteriaType25))
                {
                    templateCriteriaDto.AnswerCategories = ((CriteriaType25)indicatorTemplate).AnswerCategories.ConvertAll(x => AnswerCategoryDto.ConvertAnswerCategoriesToDto(x));
                    templateCriteriaDto.AnswerCategories.Add(new AnswerCategoryDto() { Text = "Off", Score = -1, Code = "Z" });
                }
            }

            if (templateCriteria.CriteriaType == CriteriaTypeEnum.CriteriaType4)
                templateCriteriaDto.CriteriaEntityField = templateCriteria.CriteriaEntityField;
          
            return templateCriteriaDto;
        }

        public static object CreateWithNameIdType(ApiController controller, IClaimsChecker claimsChecker,BaseCriteria baseCriteria)
        {
            if (baseCriteria == null)
                throw new ArgumentNullException("BaseCriteria", "CriteriaLevel2 cannot be null.");

            if (baseCriteria.CriteriaType == CriteriaTypeEnum.CriteriaType4)
            {
                return CreateWithNameIdTypeIsEnabled(baseCriteria, ValidatePiIndicator(controller, baseCriteria.CriteriaEntityField.Split(' ')[0], claimsChecker));
            }

            return new
            {
                Id = baseCriteria.Id,
                Name = baseCriteria.Name,
                Category = baseCriteria.Category,
            };
        }

        public static bool ValidatePiIndicator(ApiController controller, string piSymbol, IClaimsChecker claimsChecker)
        {
            return claimsChecker.ValidateCriteria(controller, "", piSymbol);
        }

        public static bool ValidateGlobalCompactIndicator(ApiController controller, string piSymbol,
            IClaimsChecker claimsChecker)
        {
            return claimsChecker.ValidateCriteria(controller, "", piSymbol);
        }

        public static object CreateWithNameIdTypeIsEnabled(BaseCriteria baseCriteria, bool isEnabled)
        {
            if (baseCriteria == null)
                throw new ArgumentNullException("BaseCriteria", "CriteriaLevel2 cannot be null.");

            return new
            {
                Id = baseCriteria.Id,
                Name = baseCriteria.Name,
                Category = baseCriteria.Category,
                Type = baseCriteria.CriteriaType,
                IsEnabled = isEnabled,
            };
        }

        public static object CreateWithNameIdTypeIsEnabled(TemplateCriteria templateCriteria, bool isEnabled, string criteriaLevel3Name)
        {
            if (templateCriteria == null)
                throw new ArgumentNullException("TemplateCriteria", "TemplateCriteria cannot be null.");

            return new
            {
                Id = templateCriteria.Id,
                Name = templateCriteria.Name,
                Type = templateCriteria.CriteriaType,
                IsEnabled = isEnabled,
                CriteriaLevel3Id = templateCriteria.CriteriaLevel3Id,
                CriteriaLevel3Name = criteriaLevel3Name
            };
        }
    }
}
